<?php
/**
 * 资源统计逻辑
 * User: wangwenfan
 * Date: 2018/6/5
 * Time: 14:15.
 */

namespace App\Service;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ResourceService
{
    /**
     * 取某个区服当日资源数据.
     *
     * @param array  $types
     * @param string $yesterdayTimestamp
     * @param string $todayTimestamp
     * @param bool   $way
     *
     * @return model
     */
    public static function getResourceData(array $types, $yesterdayTimestamp, $todayTimestamp, $way = true)
    {
        $where = $way ? '<' : '>';
        $db = DB::reconnect('mysql_game_log')->table('log_money_change');
        $data = $db->whereIn('moneytype', $types)
            ->where('changeCount', $where, 0)
            ->whereBetween('time', [$yesterdayTimestamp, $todayTimestamp])
            ->selectRaw('sum(changeCount) as counts,moneytype,changetype')
            ->groupBy('changetype')->get();

        return $data;
    }

    /**
     * 库存资源.
     *
     * @return bool|model
     */
    public static function getInventory()
    {
        $db = DB::reconnect('mysql_game')->table('player_gamedata');
        $inventorys = $db->selectRaw('sum(coin) as coins,sum(stone) as stones,sum(treasure) as treasures,sum(gold)
                      as golds')->get();
        if ($inventorys->isEmpty()) {
            return false;
        }

        return $inventorys;
    }
}
